﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using GCC.APIContainer.Utilities;
using GCC.Container.Abstracts;
using GCC.Container.Models;

using Microsoft.AspNetCore.Mvc;

using tcm.entity;
using tcm.request;
using tcm.response;
using tcm.utils;

namespace tcm.Controller
{
    [ApiController]
    [Route("tcm/site")]
    public class SiteController : GCCAPIControllerBase
    {
        public SiteController(AbsGCCContext context) : base(context)
        {
        }

        [HttpGet("queryAll")]
        public DataResponse<List<Site>> queryAll()
        {
            List<Site> list = GCCEntrance.DBManager.GlobalDB.Select<Site>().Where(a => a.status == 1).
                OrderByDescending(a => a.create_date).ToList();
            return HandleData(list);
        }

        [HttpGet("query")]
        public DataResponse<List<Site>> query([FromQuery] SiteQueryReq req)
        {
            List<Site> list = GCCEntrance.DBManager.GlobalDB.Select<Site>().WhereIf(req.id != null,a =>a.id == req.id).
                WhereIf(!String.IsNullOrEmpty(req.site_name), a=> a.site_name.Contains(req.site_name)).
                WhereIf(req.site_no != null, a => a.site_no == req.site_no).
                WhereIf(req.is_using != null, a => a.is_using == req.is_using).
                WhereIf(req.status != null, a => a.status == req.status).
                WhereIf(req.beginTime != null, a => a.deploy_date >= req.beginTime).
                WhereIf(req.endTime != null, a => a.deploy_date <= req.endTime)
                .Where(a => a.status == 1)
                .OrderByDescending(a => a.create_date)
                .Count(out var count)
                .Page(req.PageIndex ?? 1, req.PageSize).ToList();
            return HandleData(list, count);
        }

        [HttpPost("add")]
        public Result add(SiteReq req) 
        {
            Result res = new Result();
            res.code = GlobalConstant.Success;
            res.msg = "新增成功！";

            List<Site> siteList = GCCEntrance.DBManager.GlobalDB.Select<Site>().Where(a => a.site_no == req.site_no).Where(a => a.status == 1).ToList();
            if (siteList != null && siteList.Count > 0) 
            {
                res.code = GlobalConstant.Failure;
                res.msg = "站点编号重复！";
                return res;
            }

            Site site = BeanUtils.copy<Site, SiteReq>(req);
            site.id = IdUtils.getId();
            site.status = 1;
            site.create_date = DateTime.Now;
            site.create_by = (long)req.sysUser;
            site.update_date = DateTime.Now;
            site.update_by = (long)req.sysUser;

            int m = GCCEntrance.DBManager.GlobalDB.Insert<Site>(site).ExecuteAffrows();
            if (m > 0)
            {
                res.id = site.id;
                res.msg = "新增成功！";
            }
            else 
            {
                res.msg = "新增失败！";
                res.code = GlobalConstant.Failure;
            }
            return res;
        }

        [HttpPut("update")]
        public Result update(SiteReq req)
        {
            Result res = new Result();
            res.code = GlobalConstant.Success;
            res.msg = "更新成功！";

            List<Site> siteList = GCCEntrance.DBManager.GlobalDB.Select<Site>().Where(a => a.id == req.id).Where(a => a.status == 1).ToList();
            if (siteList == null || siteList.Count == 0)
            {
                res.code = GlobalConstant.Failure;
                res.msg = "更新数据不存在！";
                return res;
            }

            Site site = siteList[0];
            BeanUtils.copy<Site, SiteReq>(req, ref site);
            site.update_date = DateTime.Now;
            site.update_by = (long)req.sysUser;

            int m = GCCEntrance.DBManager.GlobalDB.Update<Site>(req.id).SetSource(site).ExecuteAffrows();

            if (m <= 0)
            {
                res.code = GlobalConstant.Failure;
                res.msg = "更新失败！";
            }
            else
            {
                res.id = site.id;
            }
            return res;
        }

        [HttpDelete("delete/{id}")]
        public Result delete([FromRoute] long id)
        {
            Result res = new Result();
            res.code = GlobalConstant.Success;
            res.msg = "删除成功！";

            List<Site> siteList = GCCEntrance.DBManager.GlobalDB.Select<Site>().Where(a => a.id == id).ToList();
            if (siteList == null || siteList.Count == 0)
            {
                res.code = GlobalConstant.Failure;
                res.msg = "删除数据不存在！";
                return res;
            }

            int m = GCCEntrance.DBManager.GlobalDB.Update<Site>(siteList[0].id).Set(a => a.status == 0).ExecuteAffrows();

            if (m <= 0)
            {
                res.code = GlobalConstant.Failure;
                res.msg = "删除失败！";
            }
            else
            {
                res.id = siteList[0].id;
            }
            return res;
        }
    }
}
